﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Threading;

namespace OmniKits.Windows.Threading
{
    public class DispatcherTimeout : DispatcherTimer
    {
        public event EventHandler Timeout;

        private void OnTick(object sender, EventArgs e)
        {
            this.Stop();
            var handler = this.Timeout;
            if (handler != null)
                handler(sender, e);
        }

        private void Initialize()
        {
            this.Tick += OnTick;
        }

        public DispatcherTimeout()
        {
            this.Initialize();
        }
        public DispatcherTimeout(DispatcherPriority priority)
            : base(priority)
        {
            this.Initialize();
        }
        public DispatcherTimeout(DispatcherPriority priority, Dispatcher dispatcher)
            : base(priority, dispatcher)
        {
            this.Initialize();
        }
        public DispatcherTimeout(TimeSpan timeout, DispatcherPriority priority, EventHandler callback, Dispatcher dispatcher)
            : base(priority, dispatcher)
        {
            if (callback == null)
                throw new ArgumentNullException(nameof(callback));

            this.Timeout += callback;
            this.Initialize();
            this.Interval = timeout;
        }
    }
}
